package vue;

import controleur.*;

import java.awt.Component;
import java.awt.Container;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;
import java.util.Observable;
import java.util.Observer;

import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextArea;
import javax.swing.table.DefaultTableModel;

import modele.BaseDeDonnees;

@SuppressWarnings("serial")
public class Fenetre extends JFrame implements Observer {
	
	// JTable
	JTable ViewerDep = null;
	JScrollPane ScrollDep = null;
	DefaultTableModel tableContentDep = null;
	
	String[] columnNamesDep = {
			"Type", 
			"Numéro",  
			"Heure du départ", 
			"Destination", 
			"Info", 
			"Numéro de la voie"
	};
	
	JTable ViewerArr = null;
	JScrollPane ScrollArr = null;
	DefaultTableModel tableContentArr = null;
	
	String[] columnNamesArr = {
			"Type", 
			"Numéro",  
			"Heure d'arrivée", 
			"Provenance", 
			"Info", 
			"Numéro de la voie"
	};
	
	/* JFrame */
	
	private GridBagConstraints Contraints = null;
	private Container Conteneur = null;
	private GridBagLayout Schema = null;
	
	private JTextArea infoArea = null;
	
	// La base de donn�es
	BaseDeDonnees Modele;
	
	public Fenetre(Observable _ObjModele)
	{
		// Modele MVC
		this.Modele = (BaseDeDonnees)_ObjModele;
		this.Modele.addObserver(this);
		this.Modele.notifyObservers();
		
		// Pour contruire la fenêtre
		Conteneur = this.getContentPane();
		Schema = new GridBagLayout();
		Conteneur.setLayout(Schema);

		Contraints = new GridBagConstraints();
		Contraints.fill = GridBagConstraints.BOTH;
		
		// Initialisation des objets
		this.IniPanDeparts();
		this.IniPanArrivee();
		
		this.IniBouttonsDep();
		this.IniBouttonsArr();
		
		// Affichage de la fenêtre
		this.setSize(600, 400);
		this.InitPanInfo();
		this.setVisible(true);
	}
	
	public void IniPanDeparts()
	{
		JLabel lablel = new JLabel("Départs");
		tableContentDep = new DefaultTableModel(columnNamesDep, 0);
		ViewerDep = new JTable(tableContentDep);
		ViewerDep.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		ScrollDep = new JScrollPane(ViewerDep);
		this.ajouterComponent(lablel, 0, 0, 1, 1, 0.1, 0.1);
		this.ajouterComponent(ScrollDep, 0, 1, 2, 2, 1, 1);
	}
	
	public void IniPanArrivee()
	{
		JLabel lablel = new JLabel("Arrivée");
		tableContentArr = new DefaultTableModel(columnNamesArr, 0);
		ViewerArr = new JTable(tableContentArr);
		ViewerArr.setAutoResizeMode(JTable.AUTO_RESIZE_ALL_COLUMNS);
		ScrollArr = new JScrollPane(ViewerArr);
		this.ajouterComponent(lablel, 2, 0, 1, 1, 0.1, 0.1);
		this.ajouterComponent(ScrollArr, 2, 1, 2, 2, 1, 1);
	}
	
	public void IniBouttonsDep()
	{
		JButton btnSupDep = new JButton("Supprimer");
		btnSupDep.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				throw new UnsupportedOperationException("Pas implémenté encore");
			}
		});
		
		JButton btnModDep = new JButton("Modifier");
		btnModDep.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int rowSelected = ViewerDep.getSelectedRow();
				if(rowSelected > -1)	{
					String[] donnees = {
							(String)tableContentDep.getValueAt(rowSelected, 0),
							(String)tableContentDep.getValueAt(rowSelected, 1),
							(String)tableContentDep.getValueAt(rowSelected, 2),
							(String)tableContentDep.getValueAt(rowSelected, 3),
							(String)tableContentDep.getValueAt(rowSelected, 4),
							(String)tableContentDep.getValueAt(rowSelected, 5)
					};
					MainControleur.modifierDep(donnees);
				};
			}
		});
		this.ajouterComponent(btnSupDep, 0, 3, 1, 1, 0.15, 0.15);
		this.ajouterComponent(btnModDep, 1, 3, 1, 1, 0.15, 0.15);
	}
	
	public void IniBouttonsArr()
	{
		JButton btnSupArr = new JButton("Supprimer");
		btnSupArr.addActionListener(new ActionListener() {			
			@Override
			public void actionPerformed(ActionEvent e) {
				throw new UnsupportedOperationException("Pas implémenté encore");
			}
		});
		
		JButton btnModArr = new JButton("Modifier");
		btnModArr.addActionListener(new ActionListener() {
			@Override
			public void actionPerformed(ActionEvent e) {
				int rowSelected = ViewerArr.getSelectedRow();
				if(rowSelected > -1)	{
					String[] donnees = {
							(String)tableContentArr.getValueAt(rowSelected, 0),
							(String)tableContentArr.getValueAt(rowSelected, 1),
							(String)tableContentArr.getValueAt(rowSelected, 2),
							(String)tableContentArr.getValueAt(rowSelected, 3),
							(String)tableContentArr.getValueAt(rowSelected, 4),
							(String)tableContentArr.getValueAt(rowSelected, 5)
					};
					MainControleur.modifierArr(donnees);
				};
			}
		});
		this.ajouterComponent(btnSupArr, 2, 3, 1, 1, 0.15, 0.15);
		this.ajouterComponent(btnModArr, 3, 3, 1, 1, 0.15, 0.15);
	}
	
	public void InitPanInfo()
	{
		JLabel lablel = new JLabel("Information général");
		lablel.setHorizontalAlignment(JLabel.CENTER);
		infoArea = new JTextArea();
		infoArea.setSize(this.getWidth(), this.getHeight() / 4);
		JScrollPane scrollInfo = new JScrollPane(infoArea);
		JButton btnModifier = new JButton("Modifier Info");
		this.ajouterComponent(lablel, 0, 4, 4, 1, 0.1, 0.1);
		this.ajouterComponent(scrollInfo, 0, 5, 4, 1, 1, 1);
		this.ajouterComponent(btnModifier, 0, 6, 4, 1, 0.15, 0.15);
	}
	
	private void ajouterComponent(Component sender, int gridCol, int gridRow, int ColSpan, int RowSpan, double width, double height)
	{
		Contraints.gridx = gridCol;
		Contraints.gridy = gridRow;
		Contraints.gridwidth = ColSpan;
		Contraints.gridheight = RowSpan;
		Contraints.weightx = width;
		Contraints.weighty = height;
		Schema.setConstraints(sender, Contraints);
		Conteneur.add(sender);
	}
	
	@Override
	public void update(Observable o, Object arg) {
		if("arr".equals(MainControleur.getTypeChange()))
		{
			// Nettoyer
			int numRows = tableContentArr.getRowCount();
			for(int i = numRows - 1; i >= 0; i--)
			{
				tableContentArr.removeRow(i);
			}
			
			@SuppressWarnings("unchecked")
			List<TrainArrivee> nwList = (List<TrainArrivee>)arg;
			String[] rows = new String[columnNamesArr.length];
			
			for(TrainArrivee unProfil : nwList)
			{
				rows[0] = unProfil.getType();
				rows[1] = Integer.toString(unProfil.getNumero());
				rows[2] = unProfil.getHeure();
				rows[3] = unProfil.getProvenance();
				rows[4] = unProfil.getInfo();
				rows[5] = Integer.toString(unProfil.getNumeroVoie());
				tableContentArr.addRow(rows);
			}
		}
		else
		{
			if("dep".equals(MainControleur.getTypeChange()))
			{
				// Nettoyer
				int numRows = tableContentDep.getRowCount();
				for(int i = numRows - 1; i >= 0; i--)
				{
					tableContentDep.removeRow(i);
				}
				
				@SuppressWarnings("unchecked")
				List<TrainDepart> nwList = (List<TrainDepart>)arg;
				String[] rows = new String[columnNamesArr.length];
				
				for(TrainDepart unProfil : nwList)
				{
					rows[0] = unProfil.getType();
					rows[1] = Integer.toString(unProfil.getNumero());
					rows[2] = unProfil.getHeure();
					rows[3] = unProfil.getDestination();
					rows[4] = unProfil.getInfo();
					rows[5] = Integer.toString(unProfil.getNumeroVoie());
					tableContentDep.addRow(rows);
				}
			}
			else
			{// info
				infoArea.setText((String)arg);
			}
		}
	}
}
